package com.hb.controller;

import com.hb.core.base.PageDataInfo;
import com.hb.core.base.Result;
import com.hb.core.enums.BaseEnums;
import com.hb.domain.user.assembler.UserDoConvert;
import com.hb.domain.user.entity.UserDo;
import com.hb.domain.user.service.ISysUserService;
import com.hb.util.Results;
import com.hb.vo.UserVO;
import com.hb.vo.request.UpdatePwdRequestVO;
import com.hb.vo.request.UserAddRequestVO;
import com.hb.vo.request.UserRequestVO;
import com.hb.vo.request.UserUpdateRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 用户
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/8/11 19:31
 * @version:1.0
 */
@RequestMapping("/user")
@RestController
@Slf4j
@Api(value = "用户管理", tags = "用户信息")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private UserDoConvert userDoConvert;

    @GetMapping(name = "分页查询用户信息", value = "/page")
    @ApiOperation(value = "分页查询用户信息", notes = "分页查询用户信息")
    public Result<PageDataInfo<UserVO>> page(@RequestBody UserRequestVO userRequestVO) {
        PageDataInfo<UserVO> list = sysUserService.pageInfo(userRequestVO);
        return Results.successWithData(list, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @GetMapping(name = "获取所有用户信息", value = "/getAll")
    @ApiOperation(value = "获取所有用户信息", notes = "获取所有用户信息")
    public Result<List<UserVO>> queryAllUser() {
        List<UserVO> list = sysUserService.getUserAll();
        return Results.successWithData(list, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @GetMapping(name = "根据用户id查询用户", value = "/getOne")
    @ApiOperation(value = "根据ID查询用户信息", notes = "根据ID查询用户信息")
    @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "String")
    public Result queryOne(@RequestParam(name = "id", required = true) String id) {
        UserVO userVO = sysUserService.getByUserId(id);
        return Results.successWithData(userVO, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @PostMapping(name = "新增用户信息", value = "/add")
    @ApiOperation(value = "新增用户信息", notes = "新增用户信息")
    public Result addUser(@RequestBody @Validated UserAddRequestVO userAddRequestVO) {
        UserDo userDo = userDoConvert.toDo(userAddRequestVO);
        sysUserService.addUser(userDo);
        return Results.success();
    }

    @PostMapping(name = "更新用户信息", value = "/edit")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    public Result updateUser(@RequestBody UserUpdateRequestVO userUpdateRequestVO) {
        UserDo userDo = userDoConvert.toDo(userUpdateRequestVO);
        sysUserService.updateUser(userDo);
        return Results.success();
    }

    @PostMapping(name = "删除用户信息", value = "/remove")
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @ApiImplicitParam(paramType = "delete", name = "id", value = "用户id", required = true, dataType = "String")
    public Result deleteUser(@RequestParam(name = "id", required = true) String id) {
        sysUserService.deleteUser(id);
        return Results.success();
    }

    @PostMapping(name = "修改用户密码", value = "/password")
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    public Result updatePassword(@RequestBody @Validated UpdatePwdRequestVO updatePwdRequestVO){
        sysUserService.updatePassword(updatePwdRequestVO);
        return Results.success();
    }

}
